<template>
	<view>
		<view class="editaddress">
			<view class="content">
				<view class="row">
					<view class="nominal">收货人</view>
					<view class="input"><input placeholder="请输入收货人姓名" v-model="addressData.name" type="text" />
					</view>
				</view>
				<view class="row">
					<view class="nominal">电话号码</view>
					<view class="input"><input placeholder="请输入收货人电话号码" v-model="addressData.phone" type="number"
							maxlength="11" /></view>
				</view>
				<view class="row">
					<view class="nominal">所在地区</view>
					<view class="input selectcity" @click="chooseLocation">
						<input placeholder="请选择省市区" disabled type="text" v-model="addressData.address" />
					</view>
				</view>
				<view class="row">
					<view class="nominal">详细地址</view>
					<view class="input"><textarea style="font-size: 28upx;" v-model="addressData.moreAddres"
							auto-height="true" placeholder="输入详细地址" v-if="visible == false"></textarea></view>
				</view>
				<view class="row">
					<view class="nominal" style="color: 666;margin-top: 10upx;">设为默认地址</view>
					<view class="input switch">
						<switch :color="colors" style="transform:scale(0.8)" @change="switchChange"
							:checked="addressData.isdefult == 1"></switch>
					</view>
				</view>
			</view>
			<view class="save">
				<view class="btn" :style="'background:' + colors" @click="saveAddress">保存地址</view>
			</view>
		</view>

	</view>
</template>

<script>
import cityPicker from '@/uni_modules/piaoyi-cityPicker/components/piaoyi-cityPicker/piaoyi-cityPicker'
export default {
	data() {
		return {
			colors: '#f23a3a',
			visible: false, // 控制选择器显示和隐藏
			maskCloseAble: true, // 点击模态框是否关闭弹框
			str: '',
			addressData: {
				name: '',
				phone: '',
				address: '',
				moreAddres: '',
				isdefult: 0,
				id: ''
			},
		}
	},
	components: {
		cityPicker
	},
	onLoad(option) {
		let title = '新增收货地址';
		if (option.type === 'edit') {
			title = '编辑收货地址'

			this.addressData = JSON.parse(option.data)
		}
		this.manageType = option.type;
		uni.setNavigationBarTitle({
			title
		})
	},
	methods: {
		// 
		cancel() {
			this.visible = false
		},
		// 地图位置选中
		chooseLocation() {
			uni.chooseLocation({
				success: (res) => {
					this.addressData.address = res.name;
				}
			});
		},
		// 页面提示
		showErrorToast(message) {
			uni.showToast({
				title: message,
				icon: 'none'
			});
		},
		/**
		 * 保存地址信息
		 * @returns {void} 无返回值
		 */
		saveAddress() {
			// 获取地址数据
			let data = this.addressData;
			
			// 验证收货人姓名是否为空
			if (!data.name) {
				this.showErrorToast('请填写收货人姓名')
				return;
			}
			
			// 验证手机号码格式是否正确
			if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.phone)) {
				this.showErrorToast('请输入正确的手机号码')
				return;
			}
			
			// 验证地址信息是否为空
			if (!data.address) {
				this.showErrorToast('请在地图选择所在位置')
				return;
			}
			
			// 验证门牌号信息是否为空
			if (!data.moreAddres) {
				this.showErrorToast('请填写门牌号信息')
				return;
			}

			// 打印地址添加或修改成功的消息
			console.log(`地址${this.manageType == 'edit' ? '修改' : '添加'}成功`);			
			// 导航到下一个页面，传递地址数据和操作类型
			uni.navigateTo({
				url: `/pages/carGots/carGots?type=${this.manageType}&data=${JSON.stringify(data)}`,
			})
		},
		switchChange() {
			// 当状态改变时触发，value为switch开关是否打开，true表示打开，false表示关闭
			this.addressData.isdefult = this.addressData.isdefult == 1 ? 0 : 1;
		}
	},
	onShareAppMessage(res) {
		if (res.from === 'button') { // 来自页面内分享按钮
			console.log(res.target)

		}
		return {
			title: 'data-cityPicker省市区地址选择器！',
			path: '/pages/cityPicker/cityPicker'
		}
	},
	onShareTimeline(res) {
		if (res.from === 'button') { // 来自页面内分享按钮
			console.log(res.target)
		}
		return {
			title: 'data-cityPicker省市区地址选择器！'
		}
	}
}
</script>
<style lang="scss" scoped>
.save {
	position: fixed;
	bottom: 0;
	width: 100%;
	height: 120upx;
	display: flex;
	justify-content: center;
	align-items: center;
}

.save view {
	display: flex;
}

.save .btn {
	box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
	width: 70%;
	height: 80upx;
	border-radius: 80upx;
	background-color: #f23a3a;
	color: #fff;
	justify-content: center;
	align-items: center;
	font-size: 30upx;
	margin-bottom: 60px;
}

.save .btn .icon {
	height: 80upx;
	color: #fff;
	font-size: 30upx;
	justify-content: center;
	align-items: center;
}

.content {
	display: flex;
	flex-wrap: wrap;
	margin-top: 40upx;
}

.content view {
	display: flex;
}

.content .row {
	width: 92%;
	margin: 0 4%;
	border-bottom: solid 1upx #eee;
}

.content .row .nominal {
	width: 30%;
	height: 80upx;
	font-size: 28upx;
	font-family: Droid Sans Fallback;
	font-weight: 400;
	color: rgba(51, 51, 51, 1);
	align-items: center;
}

.content .row .input {
	width: 70%;
	padding: 20upx 0;
	align-items: center;
	font-size: 28upx;

}

.content .row .input input {
	font-size: 28upx;
	color: #333333;
}

.content .row .switch {
	justify-content: flex-end;
}

.content .row .input textarea {
	min-height: 40upx;
	line-height: 40upx;
}

.content .del_box {
	width: 100%;
	display: block;
	overflow: hidden;
	margin-right: 30upx;
}

.content .del {
	width: 240upx;
	height: 80upx;
	float: right;
	justify-content: center;
	align-items: center;
	font-size: 28upx;
	color: #F23A3A;
	margin: 0 auto;
	margin-top: 50upx;
	border-radius: 38upx;
	background-color: rgba(255, 0, 0, 0.05);
	border-bottom: solid 1upx #eee;
}

.selectcity input {
	width: 90%;
}

.selectcity image {
	width: 40upx;
	height: 40upx;
	float: right;
}
</style>